import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types';
import { getWeekMonthQuarterYear } from '/@/utils';
import { hasScopePlaceholder } from 'unocss';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '售后工单号',
    align: 'left',
    dataIndex: 'workorderinfo',
    slots: { customRender: 'workorderSlots' },
    width: 80,
  },

  {
    title: '售后信息',
    align: 'center',
    dataIndex: 'aftersaleinfo',
    width: 100,
    slots: { customRender: 'aftersaleinfo' },
    resizable: true,
  },
  {
    title: '售后单状态',
    align: 'center',
    dataIndex: 'woStatus_dictText',
    ifShow: false,
  },
  {
    title: '售后类型',
    align: 'center',
    dataIndex: 'aftersaleType_dictText',
    ifShow: false,
  },
  {
    title: '售后原因',
    align: 'center',
    dataIndex: 'renson_dictText',
    ifShow: false,
  },
  {
    title: '售后数据',
    align: 'center',
    dataIndex: 'aftersaleProductinfo',
    width: 90,
    slots: { customRender: 'aftersaleProductinfo' },
    resizable: true,
  },
  {
    title: '订单信息',
    align: 'center',
    dataIndex: 'orderinfo',
    width: 100,
    slots: { customRender: 'orderinfo' },
    resizable: true,
  },
  {
    title: '售后备注',
    align: 'center',
    dataIndex: 'woNote',
    width: 100,
  },
  {
    title: '创建人',
    align: 'center',
    dataIndex: 'create_by',
    ifShow: false,
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'create_time',
    ifShow: false,
  },
  {
    title: '订单单号',
    align: 'center',
    dataIndex: 'orderCode',
    ifShow: false,
  },
  {
    title: '门店编号',
    align: 'center',
    dataIndex: 'storeCode',
    ifShow: false,
  },
  {
    title: '售后商品总数量',
    align: 'center',
    dataIndex: 'totalNum',
    ifShow: false,
  },
  {
    title: '售后商品种数',
    align: 'center',
    dataIndex: 'detailNum',
    ifShow: false,
  },
  {
    title: '售后总金额',
    align: 'center',
    dataIndex: 'totalAmount',
    ifShow: false,
  },
  {
    title: '售后凭证',
    align: 'center',
    dataIndex: 'woImage',
    customRender: render.renderImage,
    width: 150,
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '售后工单号',
    field: 'workorder',
    component: 'JInput',
  },
  {
    label: '订单编号',
    field: 'orderCode',
    component: 'JInput',
  },
  {
    label: '门店编号',
    field: 'storeCode',
    component: 'JInput',
  },
  {
    label: '售后单状态',
    field: 'woStatus',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'audit_state',
    },
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '售后工单号',
    field: 'workorder',
    component: 'Input',
    dynamicDisabled: true,

    componentProps: {
      placeholder: '系统自动生成',
    },
  },
  {
    label: '订单编号',
    field: 'orderCode',
    component: 'Input',
    dynamicDisabled: true,
  },
  {
    label: '门店编号',
    field: 'storeCode',
    component: 'Input',
    dynamicDisabled: true,
    ifShow: false,
  },
  {
    label: '门店编号',
    field: 'storeName',
    component: 'Input',
    dynamicDisabled: true,
  },

  {
    label: '售后类型',
    field: 'aftersaleType',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'slt_aftersale_type',
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择售后类型!' }];
    },
  },
  // {
  //   label: '售后原因',
  //   field: 'renson',
  //   component: 'JDictSelectTag',
  //   componentProps: {
  //     dictCode: 'slt_aftersale_renson',
  //   },
  //   dynamicRules: ({ model, schema }) => {
  //     return [{ required: false, message: '请选择售后原因!' }];
  //   },
  // },
  {
    label: '售后备注',
    field: 'woNote',
    component: 'InputTextArea',
    //属性
    componentProps: {
      //可以点击清除图标删除内容
      allowClear: true,
      //是否展示字数
      showCount: true,
      //自适应内容高度，可设置为 true | false 或对象：{ minRows: 2, maxRows: 6 }
      autoSize: {
        //最小显示行数
        minRows: 2,
        //最大显示行数
        maxRows: 3,
      },
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入售后备注!' }];
    },
  },
  {
    label: '售后凭证',
    field: 'woImage',
    component: 'JImageUpload',
    componentProps: {
      fileMax: 6,
      bizPath: 'aftersale',
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请上传售后凭证!' }];
    },
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

//子表表格
export const subColumns: BasicColumn[] = [
  {
    title: '主图',
    dataIndex: 'mainPic',
    width: '50px',
    customRender: render.renderImage,
  },
  {
    title: 'upc_code',
    dataIndex: 'upcCode',
    width: '150px',
  },
  {
    title: '商品名称',
    align: 'center',
    dataIndex: 'productName',
  },
  {
    title: '产品价格',
    align: 'center',
    dataIndex: 'productPrice',
  },
  {
    title: '售后数量',
    align: 'center',
    dataIndex: 'aftersaleQuantity',
  },
  {
    title: '售后金额',
    align: 'center',
    dataIndex: 'aftersaleAmount',
  },
  {
    title: '售后原因',
    align: 'center',
    dataIndex: 'reason_dictText',
  },
];
//子表表格配置
export const xlCustomerAftersaleDetailColumns: JVxeColumn[] = [
  {
    title: '主图',
    key: 'mainPic',
    type: JVxeTypes.image,
    width: '50px',
  },
  {
    title: 'upc_code',
    key: 'upcCode',
    type: JVxeTypes.normal,
    width: '150px',
  },
  {
    title: '商品名称',
    key: 'productName',
    type: JVxeTypes.normal,
    width: '200px',
  },
  {
    title: '产品价格',
    key: 'productPrice',
    type: JVxeTypes.normal,
    width: '100px',
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '订单数量',
    key: 'orderQuantity',
    type: JVxeTypes.normal,
    width: '120px',
  },
  {
    title: '订单金额',
    key: 'orderAmount',
    type: JVxeTypes.normal,
    width: '120px',
  },
  {
    title: '售后数量',
    key: 'aftersaleQuantity',
    type: JVxeTypes.inputNumber,
    width: '120px',
    placeholder: '请输入${title}',
    defaultValue: '',
    validateRules: [
      {
        handler({ cellValue, row, column }, callback, target) {
          if (Number(cellValue) <= 0) {
            //callback(false, '${title}不能小于等于0'); 如果不填或作写0表示无售后
          }
          if (Number(cellValue) > Number(row['orderQuantity'])) {
            callback(false, '${title}不能大于订单数量');
          } else {
            callback(true); // true = 通过验证
          }
        },
        message: '${title}默认提示',
      },
    ],
  },
  {
    title: '售后金额',
    key: 'aftersaleAmount',
    type: JVxeTypes.normal,
    width: '120px',
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '售后原因',
    key: 'reason',
    type: JVxeTypes.select,
    dictCode: 'slt_aftersale_renson',
    placeholder: '请输入${title}',
    defaultValue: '',
    validateRules: [
      {
        handler({ cellValue, row, column }, callback, target) {
          if (row.aftersaleAmount > 0) {
            if (!cellValue) {
              callback(false, '${title} 必填');
            } else callback(true);
          } else callback(true);
        },
        message: '${title}默认提示',
      },
    ],
  },
];

//审批表单
export const auditFormSchema: FormSchema[] = [
  {
    label: '审批状态',
    field: 'woStatus',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'form_audit_state',
      type: 'radioButton',
    },
    defaultValue: '1',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择审批状态!' }];
    },
  },
  {
    label: '审批说明',
    field: 'woNote',
    component: 'InputTextArea',
    //属性
    componentProps: {
      //可以点击清除图标删除内容
      allowClear: true,
      //是否展示字数
      showCount: true,
      //自适应内容高度，可设置为 true | false 或对象：{ minRows: 2, maxRows: 6 }
      autoSize: {
        //最小显示行数
        minRows: 2,
        //最大显示行数
        maxRows: 3,
      },
    },
    ifShow: ({ values }) => values.woStatus === '2',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入审批说明!' }];
    },
  // TODO 主键隐藏字段，目前写死为ID
  },
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

// 高级查询数据
export const superQuerySchema = {
  workorder: { title: '售后工单号', order: 0, view: 'text', type: 'string' },
  woStatus: { title: '售后单状态（0:待审批，1:审批通过,2:审批不通过）', order: 1, view: 'number', type: 'number', dictCode: 'audit_state' },
  woNote: { title: '备注', order: 2, view: 'text', type: 'string' },
  aftersaleType: { title: '售后类型', order: 3, view: 'list', type: 'string', dictCode: 'slt_aftersale_type' },
  renson: { title: '售后原因', order: 4, view: 'list', type: 'string', dictCode: 'slt_aftersale_renson' },
  orderCode: { title: '订单单号', order: 5, view: 'text', type: 'string' },
  storeCode: { title: '门店编号', order: 6, view: 'text', type: 'string' },
  totalNum: { title: '售后商品总数量', order: 7, view: 'number', type: 'number' },
  detailNum: { title: '售后商品种数', order: 8, view: 'number', type: 'number' },
  totalAmount: { title: '售后总金额', order: 9, view: 'number', type: 'number' },
  woImage: { title: '售后凭证', order: 10, view: 'image', type: 'string' },
  //子表高级查询
  xlCustomerAftersaleDetail: {
    title: '售后单明细',
    view: 'table',
    fields: {
      workorder: { title: '售后单号', order: 0, view: 'text', type: 'string' },
      orderId: { title: 'orderId', order: 1, view: 'text', type: 'string' },
      orderCode: { title: '订单编码', order: 2, view: 'text', type: 'string' },
      productId: { title: '商品ID', order: 3, view: 'text', type: 'string' },
      productPrice: { title: '产品价格', order: 4, view: 'number', type: 'number' },
      aftersaleType: { title: '售后类型', order: 5, view: 'number', type: 'number' },
      tenantId: { title: '租户号', order: 6, view: 'text', type: 'string' },
      aftersaleQuantity: { title: '售后数量', order: 7, view: 'number', type: 'number' },
      aftersaleAmount: { title: '售后金额', order: 8, view: 'number', type: 'number' },
    },
  },
};

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
